package com.newcapec.modules.iot.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.newcapec.common.utils.ConfigConstant;
import com.newcapec.modules.health.entity.CustomerEntity;
import com.newcapec.modules.health.entity.StudentEntity;
import com.newcapec.modules.health.service.CustomerService;
import com.newcapec.modules.health.service.StudentService;
import com.newcapec.modules.iot.service.IotApiService;
import com.newcapec.modules.iot.utils.IotErrorCode;
import com.newcapec.modules.iot.utils.IotResponse;
import com.newcapec.modules.pass.entity.*;
import com.newcapec.modules.pass.service.*;
import com.newcapec.modules.sys.service.SysConfigService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("iotApiService")
public class IotApiServiceImpl implements IotApiService {

    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private GateInfoService gateInfoService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private PassInfoService passInfoService;
    @Autowired
    private PassRecordService passRecordService;
    @Autowired
    private WhiteListService whiteListService;
    @Autowired
    private WhiteListContentService whiteListContentService;
    @Autowired
    private TemporaryAccessService temporaryAccessService;

    /**
     * 时间校对
     */
    @Override
    public IotResponse getISOTime(Object params) {
        final String format = DateUtil.format(new Date(),"yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        final JSONObject sysDate = new JSONObject();
        IotResponse responseJson = new IotResponse();
        responseJson.setRetcode(-100);
        responseJson.setRetmsg("调用getISOTime接口发生异常");
        final JSONObject paramJson = (JSONObject) JSONObject.toJSON(params);
        String machinesId = paramJson.getString("poscode");
        try {
            sysDate.put("date", format);
            sysDate.put("poscode", machinesId);
            responseJson.setRetcode(200);
            responseJson.setRetmsg("成功");
        } catch (Exception e) {
            sysDate.put("errcode", -1);
            responseJson.setRetmsg("调用getISOTime接口发生异常：" + e.getMessage());
            e.printStackTrace();
        }
        responseJson.setParam(sysDate);
        return responseJson;
    }

    /**
     * 根据学校编号获取学校信息和出入口信息
     */
    @Override
    public IotResponse getGateList(IotResponse iotResponse, JSONObject paramJson) {
        Long customerId = paramJson.getLong("customerId");
        if (customerId == null) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }

        CustomerEntity customer = customerService.get(customerId);
        List<GateInfoEntity> gateInfoList = gateInfoService.list(
                new QueryWrapper<GateInfoEntity>()
                        .eq("customer_id", customerId)
        );
        if (customer == null) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_SCHOOL_CODE);
        }

        JSONArray gates = new JSONArray();
        for (GateInfoEntity gateInfo : gateInfoList) {
            JSONObject oneJson = new JSONObject();
            oneJson.put("buildingId", gateInfo.getId());
            oneJson.put("gate", gateInfo.getBuilding() + gateInfo.getGate() + "-" + (gateInfo.getType()==1?"进":"出"));
            oneJson.put("type", gateInfo.getType());
            gates.add(oneJson);
        }
        final JSONObject dataJson = new JSONObject();
        dataJson.put("gates", gates);
        dataJson.put("schoolName", customer.getName());
        dataJson.put("qrcodeUrl", sysConfigService.getValue(ConfigConstant.QRCODE_URL));

        iotResponse.setParam(dataJson);
        return iotResponse;
    }

    /**
     * 设置出入口的机器信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public IotResponse configGate(IotResponse iotResponse, JSONObject paramJson) {
        String machinesId = paramJson.getString("machinesId");
        if (StringUtils.isBlank(machinesId)) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }
        String machinesType = paramJson.getString("machinesType");
        if (StringUtils.isBlank(machinesType)) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }
        Long buildingId = paramJson.getLong("buildingId");
        if (buildingId == null) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }

        GateInfoEntity gateInfo = gateInfoService.get(buildingId);
        if (gateInfo == null) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_JIJU_RELA_CODE);
        }
        gateInfo.setMachinesId(machinesId);
        gateInfo.setMachinesType(machinesType);
        gateInfoService.edit(gateInfo);

        final JSONObject dataJson = new JSONObject();
        dataJson.put("code", 0);
        dataJson.put("msg", "已绑定到" + gateInfo.getBuilding() + gateInfo.getGate() + "-" + (gateInfo.getType()==1?"进":"出"));
        iotResponse.setParam(dataJson);
        return iotResponse;
    }

    /**
     * 确认覆盖出入口信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public IotResponse confirmCoverGate(IotResponse iotResponse, JSONObject paramJson) {
        String machinesId = paramJson.getString("machinesId");
        if (StringUtils.isBlank(machinesId)) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }
        String machinesType = paramJson.getString("machinesType");
        if (StringUtils.isBlank(machinesType)) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }
        Long buildingId = paramJson.getLong("buildingId");
        if (buildingId == null) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }

        GateInfoEntity gateInfo = gateInfoService.get(buildingId);
        if (gateInfo == null) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_JIJU_RELA_CODE);
        }
        gateInfo.setMachinesId(machinesId);
        gateInfo.setMachinesType(machinesType);
        gateInfoService.edit(gateInfo);

        final JSONObject dataJson = new JSONObject();
        dataJson.put("code", 0);
        dataJson.put("msg", "已绑定到" + gateInfo.getBuilding() + gateInfo.getGate() + "-" + (gateInfo.getType()==1?"进":"出"));
        iotResponse.setParam(dataJson);
        return iotResponse;
    }

    /**
     * 设备扫二维码
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public IotResponse scanPersonalQrCode(IotResponse iotResponse, JSONObject paramJson) {
        /**
         * 获取请求参数
         * qrData 二维码字符串
         * stuNo 二维码字符串中携带的学生学号参数
         * customerId 二维码字符串中携带的学校编号参数
         * machinesId 设备编号
         * machinesType 设备类型
         */
        String qrData = paramJson.getString("qrData");
        if (StringUtils.isBlank(qrData)) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }
        Map<String, String> map;
        try {
            map = JSON.parseObject(qrData,Map.class);
        } catch (Exception e) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_QR_CODE);
        }
        String stuNo = map.get("stuNo");
        if (StringUtils.isBlank(stuNo)) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }
        String customerId = String.valueOf(map.get("customerId"));
        if (StringUtils.isBlank(customerId)) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }
        String machinesId = paramJson.getString("machinesId");
        if (StringUtils.isBlank(machinesId)) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }
        String machinesType = paramJson.getString("machinesType");
        if (StringUtils.isBlank(machinesType)) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }

        /**
         * 获取出入口信息
         */
        GateInfoEntity gateInfo = gateInfoService.getOne(
                new QueryWrapper<GateInfoEntity>()
                        .eq("machines_id", machinesId)
                        .eq("machines_type", machinesType)
                        .eq("customer_id", customerId)
        );
        if (gateInfo == null) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_JIJU_RELA_CODE);
        }

        /**
         * 获取学生信息
         */
        StudentEntity studentEntity = studentService.getOne(
                new QueryWrapper<StudentEntity>()
                        .eq("stu_no", stuNo)
                        .eq("customer_id", customerId)
        );
        if (studentEntity == null) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_STUDENT_INFO);
        }

        /**
         * 获取认证信息
         */
        PassInfoEntity passInfoEntity = passInfoService.getByStudentId(studentEntity.getId());
        if (passInfoEntity == null) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_PASS_INVALID);
        }

        /**
         * 是否有权限进入
         */
        if(!passRule(stuNo, gateInfo)){
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_NO_AUTHORITY);
        }

        PassRecordEntity passRecordEntity = new PassRecordEntity();
        passRecordEntity.setCustomerId(gateInfo.getCustomerId());
        passRecordEntity.setGateId(gateInfo.getId());
        passRecordEntity.setStudentId(studentEntity.getId());
        passRecordEntity.setScanType(1);
        passRecordEntity.setActionType(0);
        passRecordService.save(passRecordEntity);

        final JSONObject dataJson = new JSONObject();
        dataJson.put("recordId",passRecordEntity.getId());
        dataJson.put("photo", passInfoEntity.getPhoto());
        dataJson.put("name", studentEntity.getName());
        dataJson.put("stu_no", studentEntity.getStuNo());
        dataJson.put("collegeName", studentEntity.getCollegeDescribe());
        iotResponse.setParam(dataJson);
        return iotResponse;
    }

    /**
     * 设备扫人脸
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public IotResponse scanFace(IotResponse iotResponse, JSONObject paramJson) {
        /**
         * 获取请求参数
         * customerId 学校编号
         * stuNo 学生学号
         * machinesId 设备编号
         * machinesType 设备类型
         * type(scanType) 扫描类型 0人扫码1机扫码2人脸
         */
        String customerId = paramJson.getString("customerId");
        if (StringUtils.isBlank(customerId)) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }
        String stuNo = paramJson.getString("stuNo");
        if (StringUtils.isBlank(stuNo)) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }
        String machinesId = paramJson.getString("machinesId");
        if (StringUtils.isBlank(machinesId)) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }
        String machinesType = paramJson.getString("machinesType");
        if (StringUtils.isBlank(machinesType)) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }
        String scanType = paramJson.getString("type");
        if (StringUtils.isBlank(scanType)) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }

        /**
         * 获取出入口信息
         */
        GateInfoEntity gateInfo = gateInfoService.getOne(
                new QueryWrapper<GateInfoEntity>()
                        .eq("machines_id", machinesId)
                        .eq("machines_type", machinesType)
                        .eq("customer_id", customerId)
        );
        if (gateInfo == null) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_JIJU_RELA_CODE);
        }

        /**
         * 获取学生信息
         */
        StudentEntity student = studentService.getOne(
                new QueryWrapper<StudentEntity>()
                        .eq("stu_no", stuNo)
                        .eq("customer_id", customerId)
        );
        if (student == null) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_STUDENT_INFO);
        }

        /**
         * 获取认证信息
         */
        PassInfoEntity passInfo = passInfoService.getByStudentId(student.getId());
        if (passInfo == null) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_PASS_INVALID);
        }

        /**
         * 是否有权限进入
         */
        if(!passRule(stuNo, gateInfo)){
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_NO_AUTHORITY);
        }

        /**
         * 生成进入记录
         */
        PassRecordEntity passRecord = new PassRecordEntity();
        passRecord.setCustomerId(gateInfo.getCustomerId());
        passRecord.setGateId(gateInfo.getId());
        passRecord.setStudentId(student.getId());
        passRecord.setActionType(0);
        passRecord.setScanType(Integer.valueOf(scanType));
        passRecord.setCreateTime(new Date());
        passRecordService.save(passRecord);

        final JSONObject dataJson = new JSONObject();
        dataJson.put("photo", passInfo.getPhoto());
        dataJson.put("name", student.getName());
        dataJson.put("stu_no", student.getStuNo());
        dataJson.put("collegeName", student.getCollegeDescribe() + student.getMajorDescribe() + student.getClassDescribe());
        dataJson.put("recordId", passRecord.getId());

        iotResponse.setParam(dataJson);
        return iotResponse;
    }

    /**
     * 提交温度
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public IotResponse submitTemperature(IotResponse iotResponse, JSONObject paramJson) {
        /**
         * 获取请求参数
         * machinesId 设备编号
         * machinesType 设备类型
         * temperature 机测体温
         * stuNo 学生学号
         * recordId 出入记录编号
         * type(scanType) 扫描类型 0人扫码1机扫码2人脸
         */
        String machinesId = paramJson.getString("machinesId");
        if (StringUtils.isBlank(machinesId)) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }
        String machinesType = paramJson.getString("machinesType");
        if (StringUtils.isBlank(machinesType)) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }
        String temperature = paramJson.getString("temperature");
        if (StringUtils.isBlank(temperature)) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }
        String type = paramJson.getString("type");
        if (StringUtils.isBlank(type)) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }
        String stuNo = paramJson.getString("stuNo");
        if (StringUtils.isBlank(stuNo)) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }
        String recordId = paramJson.getString("recordId");
        if (StringUtils.isBlank(recordId)) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }

        /**
         * 更新出入记录
         * 如果机器测温值大于等于体温阈值，禁行
         */
        PassRecordEntity passRecordEntity = passRecordService.getById(recordId);
        passRecordEntity.setMachineTemperature(new BigDecimal(temperature));
        passRecordEntity.setMachineTime(new Date());
        double temperatureThreshold = Double.parseDouble(sysConfigService.getValue(ConfigConstant.TEMPERATURE_THRESHOLD));
        if(temperatureThreshold <= passRecordEntity.getMachineTemperature().doubleValue()){
            passRecordEntity.setActionType(1);
        }
        passRecordService.updateById(passRecordEntity);

        final JSONObject dataJson = new JSONObject();
        dataJson.put("code", 0);
        dataJson.put("msg", "提交成功");

        iotResponse.setParam(dataJson);
        return iotResponse;
    }

    /**
     * 扫描机器码
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public IotResponse scanMachineQrCode(JSONObject paramJson) {
        IotResponse iotResponse = new IotResponse();
        /**
         * 获取请求参数
         * customerId 学校编号
         * gateId 出入口编号
         * studentId 学生编号
         * stuNo 学生学号
         * temperature 手填体温
         */
        Long customerId = paramJson.getLong("customerId");
        if (customerId == null) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }
        Long gateId = paramJson.getLong("gateId");
        if (gateId == null) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }
        Long studentId = paramJson.getLong("studentId");
        if (studentId == null) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }
        String stuNo = paramJson.getString("stuNo");
        if (StringUtils.isBlank(stuNo)) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }
        String temperature = paramJson.getString("temperature");
        if (StringUtils.isBlank(temperature)) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_ARGUMENT);
        }

        /**
         * 获取出入口信息
         */
        GateInfoEntity gateInfo = gateInfoService.get(gateId);
        if (gateInfo == null) {
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_JIJU_RELA_CODE);
        }

        /**
         * 是否有权限进入
         */
        if(!passRule(stuNo, gateInfo)){
            return IotErrorCode.responseMsg(iotResponse, IotErrorCode.ERROR_NO_AUTHORITY);
        }

        PassRecordEntity passRecordEntity = new PassRecordEntity();
        passRecordEntity.setCustomerId(customerId);
        passRecordEntity.setGateId(gateId);
        passRecordEntity.setStudentId(studentId);
        passRecordEntity.setScanType(0);
        passRecordEntity.setStuTemperature(new BigDecimal(temperature));
        passRecordEntity.setStuTime(new Date());
        double temperatureThreshold = Double.parseDouble(sysConfigService.getValue(ConfigConstant.TEMPERATURE_THRESHOLD));
        if(temperatureThreshold <= passRecordEntity.getStuTemperature().doubleValue()){
            passRecordEntity.setActionType(1);
        }else{
            passRecordEntity.setActionType(0);
        }
        passRecordService.save(passRecordEntity);
        return iotResponse;
    }

    /**
     * 通行规则：0通行，1禁行
     */
    private boolean passRule(String stuNo, GateInfoEntity gateInfo){
        boolean actionType  = true;

        //判断出入口黑白名单是否开启
        int whiteListSwitch = gateInfo.getWhiteListSwitch();
        if(whiteListSwitch == 1){
            /*
             * 黑白名单开启：
             * 1.先根据有效临时出入判断
             * 2.如果禁行再根据黑白名单判断，如果通行无需判断黑白名单
             * 3.名单和临时出入都通行后，根据测温判断
             */
            actionType = temporaryAccessRule(stuNo, gateInfo);
            //有效临时出入禁行后，再根据黑白名单判断
            if(!actionType){
                //获取策略
                int rule = gateInfo.getRule();
                if(rule == 1){
                    //1.全部禁行（1）时，出入口白（1）名单: 没有此人-禁行，有此人-通行
                    actionType = whiteListRule(stuNo, gateInfo.getId());
                }else if(rule == 0){
                    //2.全部通行（0）时，出入口黑（2）名单: 有此人-禁行，没有此人-通行
                    actionType = blackListRule(stuNo, gateInfo.getId());
                }
            }
        }
        return actionType;
    }

    /**
     * 白名单: 没有此人 false，有此人 true
     */
    private boolean whiteListRule(String stuNo, Long gateInfoId){
        GateInfoEntity gateInfo = gateInfoService.get(gateInfoId);
        //获取当前出入口关联的黑白名单ID
        List<Long> listIds = gateInfo.getListIds();
        if(listIds !=null && !listIds.isEmpty()){
            //获取当前出入口关联的黑白名单列表
            List<WhiteListEntity> whiteLists = whiteListService.listByIds(listIds);
            //在关联黑白名单列表中过滤，仅取出有效白名单的ID
            List<Long> validListIds = whiteLists.stream()
                    .filter(item -> item.getListType() == 1)
                    .map(WhiteListEntity::getId)
                    .collect(Collectors.toList());
            //如果有效白名单id列表为空：表示无此人
            if(validListIds==null || validListIds.isEmpty()){
                return false;
            }
            //在有效白名单中获取学生学号
            QueryWrapper<WhiteListContentEntity> whiteListContentEntityQueryWrapper = new QueryWrapper<>();
            whiteListContentEntityQueryWrapper.in("list_id", validListIds);
            List<String> stuNoList = whiteListContentService.list(whiteListContentEntityQueryWrapper)
                    .stream()
                    .map(WhiteListContentEntity::getStuNo)
                    .collect(Collectors.toList());
            //在有效白名单学号中判断是否存在当前通行学生
            if(stuNoList.contains(stuNo)){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    /**
     * 黑名单: 没有此人 true，有此人 false
     */
    private boolean blackListRule(String stuNo, Long gateInfoId){
        GateInfoEntity gateInfo = gateInfoService.get(gateInfoId);
        //获取当前出入口关联的黑白名单ID
        List<Long> listIds = gateInfo.getListIds();
        if(listIds !=null && !listIds.isEmpty()){
            //获取当前出入口关联的黑白名单列表
            List<WhiteListEntity> whiteLists = whiteListService.listByIds(listIds);
            //在关联黑白名单列表中过滤，仅取出有效黑单的ID
            List<Long> validListIds = whiteLists.stream()
                    .filter(item -> item.getListType() == 2)
                    .map(WhiteListEntity::getId)
                    .collect(Collectors.toList());
            //如果有效黑名单id列表为空：表示无此人
            if(validListIds==null || validListIds.isEmpty()){
                return true;
            }
            //在有效黑名单中获取学生学号
            QueryWrapper<WhiteListContentEntity> whiteListContentEntityQueryWrapper = new QueryWrapper<>();
            whiteListContentEntityQueryWrapper.in("list_id", validListIds);
            List<String> stuNoList = whiteListContentService.list(whiteListContentEntityQueryWrapper)
                    .stream()
                    .map(WhiteListContentEntity::getStuNo)
                    .collect(Collectors.toList());
            //在有效黑名单学号中判断是否存在当前通行学生
            if(stuNoList.contains(stuNo)){
                return false;
            }else{
                return true;
            }
        }else{
            return true;
        }
    }

    /**
     * 临时出入申请
     */
    private boolean temporaryAccessRule(String stuNo, GateInfoEntity gateInfo){
        //通过出入口id获取临时出入申请
        QueryWrapper<TemporaryAccessEntity> temporaryAccessEntityQueryWrapper = new QueryWrapper<>();
        temporaryAccessEntityQueryWrapper.eq("stu_no", stuNo);
        temporaryAccessEntityQueryWrapper.eq("gate_id", gateInfo.getId());
        List<TemporaryAccessEntity> temporaryAccessList = temporaryAccessService.list(temporaryAccessEntityQueryWrapper);
        Date today = new Date();
        for (TemporaryAccessEntity entity : temporaryAccessList) {
            if(entity.getStatus() == 1 && entity.getBeginTime().before(today)
                    && entity.getEndTime().after(today)){
                return true;
            }
        }
        return false;
    }
}
